# -*- coding: utf-8 -*-
# $Id: addbatchdialog.py 14 2009-02-14 22:24:36Z gkuznets $

# mpcheckup
# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4.QtGui import QDialog, QLineEdit, QIntValidator, QRegExpValidator
from PyQt4.QtGui import QGridLayout, QLabel, QPushButton, QMessageBox
from PyQt4.QtCore import QRegExp, SIGNAL
from core.batch import Batch

class AddBatchDialog(QDialog):
    """Диалог добавления новой пачки работ"""
  
    def __init__(self):
        QDialog.__init__(self)
        self.no_edit = QLineEdit("00");
        self.no_edit.setValidator(QRegExpValidator(QRegExp("^\d\d$"), self.no_edit))
        self.pcount_edit = QLineEdit("00")
        self.pcount_edit.setValidator(QIntValidator(0,1000,self.pcount_edit))
        self.ok_btn = QPushButton("OK")
        self.cancel_btn = QPushButton(u"Отмена")
        
        layout = QGridLayout()
        layout.addWidget(QLabel(u"Номер:"), 0,0)
        layout.addWidget(self.no_edit, 0,1)
        layout.addWidget(QLabel(u"Работ:"), 1,0)
        layout.addWidget(self.pcount_edit, 1,1)
        layout.addWidget(self.ok_btn, 2,0)
        layout.addWidget(self.cancel_btn, 2,1)
        
        self.setLayout(layout)
        self.setWindowTitle(u"Добавление пачки")
        self.connect(self.ok_btn, SIGNAL("clicked()"), self.accept)
        self.connect(self.cancel_btn, SIGNAL("clicked()"), self.reject)
  
    def exec_(self):
        # Наводим фокус в окошко имени
        self.no_edit.setSelection(0, 2)
        self.no_edit.setFocus()
        return QDialog.exec_(self)
  
    def accept(self):
        if self.pcount_edit.text().toInt()[0] == 0:
            QMessageBox.critical(self, u"Ошибка!", u"Введите ненулевое число работ")
            return
        if not Batch.find(self.no_edit.text().toInt()[0]):
            # Для добавления пачки достаточно вызвать конструктор
            b = Batch(self.no_edit.text().toInt()[0], self.pcount_edit.text().toInt()[0])
        else:
            pass
        QDialog.accept(self)
